/*
    10. Write a program in C to search an existing element in a singly linked list.
    Test Data and Expected Output :

    Input the number of nodes : 3                                                                                
                                                                                                                
    Input data for node 1 : 2                                                                                    
    Input data for node 2 : 5                                                                                    
    Input data for node 3 : 8                                                                                    
                                                                                                                
    Data entered in the list are :                                                                               
    Data = 2                                                                                                     
    Data = 5                                                                                                     
    Data = 8                                                                                                     
                                                                                                                
    Input the element to be searched : 5                                                                         
    Element found at node 2 
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List next;
} Node;

List createList(int n)
{
    List root = NULL;
    List newNode = NULL;
    List curNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        newNode->next = NULL;
        printf("Input data of node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            root = newNode;
            curNode = root;
        }else{
            curNode->next = newNode;
            curNode = newNode;
        }
    }
    return root;
}

void printList(List list)
{
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }
    while(list != NULL){
        printf("Data = %d\n", list->x);
        list = list->next;
    }    
}

int searchNode(List root, int val)
{
    List curNode = root;
    int num = 0;
    while(curNode != NULL){
        num++;
        if(curNode->x == val){
            return num;
        }
        curNode = curNode->next;
    }
}

int main(void)
{
    List root = NULL;
    int n, val;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createList(n);
    printf("\nData entered in the list are : \n");
    printList(root);
    printf("\nInput the element to be searched : ");
    scanf("%d", &val);
    printf("Element found at node %d\n", searchNode(root, val));

    return 0;
}